package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.dto.UpdateCartNumRequest;
import com.mall.shopping.dto.UpdateCartNumResponse;
import com.mall.user.constants.SysRetCodeConstants;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
public class UpdateCartsController {
    @Reference(check = false)
    ICartService iCartService;
    @PutMapping("/shopping/carts")
    public ResponseData updateCarts(@RequestBody Map<String, Object> map, HttpServletResponse response) {
        try {
            Long userId = Long.valueOf((String) map.get("userId"));
            Long itemId = Long.valueOf((Integer) map.get("productId"));
            Integer num = (Integer) map.get("productNum");
            String checked = (String) map.get("checked");

            UpdateCartNumRequest request = new UpdateCartNumRequest();
            request.setChecked(checked);
            request.setUserId(userId);
            request.setItemId(itemId);
            request.setNum(num);
            UpdateCartNumResponse numResponse = iCartService.updateCartNum(request);
            return new ResponseUtil().setData(numResponse.getMsg());
        } catch (Exception e) {
            return new ResponseUtil().setErrorMsg(SysRetCodeConstants.REQUEST_CHECK_FAILURE.getMessage());
        }
    }
}
